import Vue from 'vue';
import App from './App';
import Tips from './common/js/tips';
import api from "./utils/api.js";
// import { SDKAPPID } from '../static/debug/GenerateTestUserSig'
import TIM from 'tim-wx-sdk'
import COS from 'cos-wx-sdk-v5'
import store from './store/index'

const tim = TIM.create({
    SDKAppID: 1400251396
})
let $bus = new Vue()
Vue.prototype.$bus = $bus
wx.$app = tim
wx.$app.registerPlugin({ 'cos-wx-sdk': COS })
wx.$store=store
Vue.prototype.$store = store
Vue.prototype.TIM = TIM;
Vue.prototype.$tim = (id) => {
    let ids = "FT_" + id;
    let cid = TIM.TYPES.CONV_C2C + ids
    store.commit("resetCurrentConversation");
    wx.$app.getConversationProfile(cid).then(res => {
        // console.log(store.state.conversation.currentMessageList)
        store.commit("updateCurrentConversation", res.data.conversation);
        store.dispatch("getMessageList");
        let url = `/pages/tim/chat/main?toAccount=${ids}`;
        wx.navigateTo({ url });
    });
    // wx.navigateTo({ url: '/pages/tim/chat/main?toAccount=FT_'+wx.getStorageSync('memberId') });
}
tim.on(TIM.EVENT.SDK_READY, onReadyStateUpdate, this)
tim.on(TIM.EVENT.SDK_NOT_READY, onReadyStateUpdate, this)
tim.on(TIM.EVENT.KICKED_OUT, event => {
    store.commit('resetGroup')
    store.commit('resetUser')
    store.commit('resetCurrentConversation')
    store.commit('resetAllConversation')
    // wx.showToast({
    //     title: '你已被踢下线',
    //     icon: 'none',
    //     duration: 1500
    // })
    // setTimeout(() => {
    //     wx.clearStorage()
    //     wx.reLaunch({
    //         url: '/pages/login/main'
    //     })
    // }, 1500)
})

// 出错统一处理
tim.on(TIM.EVENT.ERROR, event => {
    // 网络错误不弹toast && sdk未初始化完全报错
    if (event.data.code !== 2800 && event.data.code !== 2999) {
        store.commit('showToast', {
            title: event.data.message,
            duration: 2000
        })
    }
})

tim.on(TIM.EVENT.MESSAGE_RECEIVED, event => {
    wx.$app.ready(() => {
        store.dispatch('onMessageEvent', event)
    })
})
tim.on(TIM.EVENT.CONVERSATION_LIST_UPDATED, event => {
    store.commit('updateAllConversation', event.data)
})
tim.on(TIM.EVENT.GROUP_LIST_UPDATED, event => {
    store.commit('updateGroupList', event.data)
})
tim.on(TIM.EVENT.BLACKLIST_UPDATED, event => {
    store.commit('updateBlacklist', event.data)
})

// tim.on(TIM.EVENT.GROUP_SYSTEM_NOTICE_RECERIVED, event => {
//     console.log('system message', event)
// })

function onReadyStateUpdate({ name }) {
    const isSDKReady = (name === TIM.EVENT.SDK_READY)
    store.commit('setSdkReady', isSDKReady)
}
function getQuery() {
    /* 获取当前路由栈数组 */
    const pages = getCurrentPages()
    const currentPage = pages[pages.length - 1]
    const options = currentPage.options
    return options
}
function getStorage(key) {
    return mpvue.getStorageSync(key);
}
function setStorage(key, data) {
    mpvue.setStorageSync(key, data);
}
//数据字典
api.getDictList().then(res => {
    mpvue.setStorageSync('dictList', res.data)
});
Vue.prototype.$api = api;
Vue.prototype.$getQuery = getQuery;
Vue.prototype.$getStorage = getStorage;
Vue.prototype.$setStorage = setStorage;
Vue.config.productionTip = false;
App.mpType = 'app';



// 全局注册Tips
Vue.prototype.$tips = Tips;
const app = new Vue(App);
app.$mount();
Vue.prototype.globalData = getApp().globalData;
